package com.whjz.dcfs.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ehsy_it on 2016/6/22.
 */
public class DateUtils {

    public static String formatDate(Date date){
        if(date!=null) {
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        }
        return "";
    }

    public static Date amount(Date date, int field, int amount) {
        Calendar calendar = Calendar.getInstance() ;
        calendar.setTime(date) ;
        calendar.add(field, amount)  ;
        return calendar.getTime()  ;
    }

    //yyyy-MM-dd
    public static Date addDayToDate(Date date, int addAmount){
        if(date!=null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date) ;
            calendar.add(Calendar.DAY_OF_MONTH, addAmount);
            return calendar.getTime();
        }
        return null;
    }

    public static Date addDayStrToDate(String dateStr,int addAmount){
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return addDayToDate(date, addAmount);
    }

    /**
     * 将dateStr日期加1天
     * @param dateStr yyyy-MM-dd类型的日期，只精确到天，可以扩展
     * @param addAmount
     * @return
     */
    public static String addDayStrToDateStr(String dateStr,int addAmount){
        Date newDate = addDayStrToDate(dateStr, addAmount);
        if(newDate!=null){
            return new SimpleDateFormat("yyyy-MM-dd").format(newDate);
        }
        return null;
    }

    /**
     * 将yyyy-MM-dd格式的日期加1天
     */
    public static String getNextDayDateStr(String dateStr) {
        return addDayStrToDateStr(dateStr,1);
    }
}
